package net.lym.service.impl;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import net.lym.cache.MerchandiseCache;
import net.lym.cache.UserCache;
import net.lym.entity.MerchandiseEntity;
import net.lym.entity.UserEntity;
import net.lym.enums.DiscountEnum;
import net.lym.service.ShoppingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 普通实现
 */
@Slf4j
@Service("shoppingService1")
public class ShoppingService1Impl implements ShoppingService {

    @Resource
    private UserCache userCache;

    @Resource
    private MerchandiseCache merchandiseCache;

    /**
     * 购买商品
     *
     * @param userId        用户ID
     * @param merchandiseId 商品ID
     * @return 购买结果
     */
    @Override
    public Map<String, Object> buy(Long userId, Long merchandiseId) {
        UserEntity user = userCache.get(userId);
        MerchandiseEntity merchandise = merchandiseCache.get(merchandiseId);
        log.info("current user: {}", JSON.toJSONString(user));
        log.info("current merchandise: {}", JSON.toJSONString(merchandise));
        DecimalFormat df = new DecimalFormat("0.00");
        DiscountEnum discountEnum = DiscountEnum.valueOf(user.getType());
        log.info("user type: {}, 享受折扣: {}", discountEnum.getType(), df.format(discountEnum.getDiscount()));
        // 普通用户
        if (user.getType() == 0) {
            Map<String, Object> map = new HashMap<String, Object>(){{
                put("用户姓名", user.getName());
                put("商品名称", merchandise.getName());
                put("商品价格", merchandise.getPrice());
                put("折后价格", merchandise.getPrice().multiply(discountEnum.getDiscount()));
            }};
            return map;
        } else
            // 普通VIP
            if (user.getType() == 1) {
                Map<String, Object> map = new HashMap<String, Object>(){{
                    put("用户姓名", user.getName());
                    put("用户性别", user.getGender());
                    put("用户等级", discountEnum.getType());
                    put("商品名称", merchandise.getName());
                    put("商品价格", merchandise.getPrice());
                    put("应享折扣", discountEnum.getDiscount());
                    put("折后价格", merchandise.getPrice().multiply(discountEnum.getDiscount()));
                }};
                return map;
            } else
                // 高级VIP
                if (user.getType() == 2) {
                    Map<String, Object> map = new HashMap<String, Object>(){{
                        put("用户姓名", user.getName());
                        put("用户年龄", user.getAge());
                        put("用户等级", discountEnum.getType());
                        put("商品名称", merchandise.getName());
                        put("商品价格", merchandise.getPrice());
                        put("应享折扣", discountEnum.getDiscount());
                        put("折后价格", merchandise.getPrice().multiply(discountEnum.getDiscount()));
                    }};
                    return map;
                } else
                    // 至尊VIP
                    if (user.getType() == 3) {
                        Map<String, Object> map = new HashMap<String, Object>(){{
                            put("用户姓名", user.getName());
                            put("用户年龄", user.getAge());
                            put("用户等级", discountEnum.getType());
                            put("商品名称", merchandise.getName());
                            put("商品价格", merchandise.getPrice());
                            put("应享折扣", discountEnum.getDiscount());
                            put("折后价格", merchandise.getPrice().multiply(discountEnum.getDiscount()));
                        }};
                        return map;
                    } else
                        // 超级VIP
                        if (user.getType() == 4) {
                            Map<String, Object> map = new HashMap<String, Object>(){{
                                put("用户ID",  user.getId());
                                put("用户姓名", user.getName());
                                put("用户性别", user.getGender());
                                put("用户年龄", user.getAge());
                                put("用户等级", discountEnum.getType());
                                put("商品名称", merchandise.getName());
                                put("商品价格", merchandise.getPrice());
                                put("应享折扣", discountEnum.getDiscount());
                                put("折后价格", merchandise.getPrice().multiply(discountEnum.getDiscount()));
                            }};
                            return map;
                        }
        return new HashMap<String, Object>();
    }

}
